import { useConsole, useToast } from '@/utils'

/** H5支付  */
export async function useH5Pay() {
  // #ifdef H5

  // #endif
}

/** 微信内H5支付  */
export async function useWechatH5Pay() {
  // #ifdef H5

  // #endif
}

/** 微信小程序支付  */
export async function useWePay() {
  // #ifdef MP-WEIXIN

  // #endif
}
interface kuaishouOptions {
  order_no?: string
  order_info_token?: string
}
interface kuaishouPaymentChannel {
  provider?: string
  provider_channel_type?: string
}
/** 快手支付 */
export async function useKuaishouPay(options: kuaishouOptions, paymentChannel?: kuaishouPaymentChannel) {
  return new Promise<void>((resolve, reject) => {
    ks.pay({
      serviceId: '1',
      orderInfo: options,
      // 目前默认支付宝支付
      paymentChannel,
      success: (res: Record<string, any>) => {
        resolve()
      },
      fail: (error: any) => {
        useConsole?.error('my.tradePay:fail', error)
        useToast('支付失败')
        reject(new Error(JSON.stringify(error)))
      },
    })
  })
}

interface AliPayOptions {
  tradeNO?: string
  orderStr?: string
}
/** 支付宝小程序支付  */
export function useAliPay(options: AliPayOptions) {
  // #ifdef MP-ALIPAY
  return new Promise<void>((resolve, reject) => {
    my.tradePay({
      ...options,
      success: (res: Record<string, any>) => {
        const resultCode = Number.parseInt(res.resultCode)
        if (resultCode === 9000) {
          resolve()
        }
        else {
          if (resultCode === 6001) {
            useToast('取消支付')
          }
          else {
            useToast('支付异常，请刷新')
          }
          useConsole?.error('my.tradePay:success:abnormal', res)
          useToast('取消支付')
          reject(new Error(JSON.stringify(res)))
        }
      },
      fail: (error: any) => {
        useConsole?.error('my.tradePay:fail', error)
        useToast('支付失败')
        reject(new Error(JSON.stringify(error)))
      },
    })
  })
  // #endif
}
